app.controller('eamicCtrl', ['$scope', '$rootScope', '$filter', 'pcdService', 'listSve', '$http', function($scope, $rootScope, $filter, pcdService, listSve, $http) {
	$rootScope.navIndex = 0;
	//获取省列表
	$scope.getPro = function() {
		pcdService.prolist().list().then(function(e) {
			$scope.prolist = e.data;
		})
		pcdService.citylist().list().then(function(e) {
			$scope.oCityList = e.data;
		})
		pcdService.dislist().list().then(function(e) {
			$scope.oDisList = e.data;
		})
	}
	$scope.getPro();
	$scope.pageInx = {}
	var pagerBox = document.getElementById('pager_1');
	//列表
	$scope.getItem = function() {
		listSve.sList().list({
			numPerPage: 20
		}).then(function(e) {
			console.info(e);
			$scope.list_1 = e.res.data;
			var count = Math.ceil(e.res.page.recordCount / 20);
			$scope.count = count;
			var pager = new Pager({
				index: 1,
				total: $scope.count,
				parent: pagerBox,
				onchange: doChangePage
			});
		});

	}
	$scope.first = true;
	$scope.getItem();
	//分页器
	function doChangePage(obj) {
		if($scope.first) {
			$scope.first = false;
		} else {
			console.info(obj.index)
			$scope.pageInx = {
				pageNum: obj.index,
				numPerPage: 20
			};
			//				$scope.getItem();
			listSve.sList().list($scope.pageInx).then(function(e) {
				console.info(e);
				$scope.list_1 = e.res.data;

			})
		}
	}

	//省市区联动
	$scope.proFunc_1 = function(e) {
		console.info(e)
		var proId = e;
		//e.data
		$scope.citylist = new Array;
		$scope.cty_1 = '';
		$scope.dis_1 = '';
		$scope.dislist = '';
		for(var i = 0; i < $scope.oCityList.length; i++) {
			if($scope.oCityList[i].pro_id == proId) {
				$scope.citylist.push($scope.oCityList[i]);
			}
		}

	}
	$scope.ctyFunc_1 = function(e) {
			var ctyId = e;
			//e.data
			$scope.dislist = new Array;
			$scope.dis_1 = '';
			for(var i = 0; i < $scope.oDisList.length; i++) {
				if($scope.oDisList[i].city_id == ctyId) {
					$scope.dislist.push($scope.oDisList[i]);
				}
			}
		}
		//导入表格
	$scope.oBaseUrl = listSve.returnBase();
	$scope.UploadFile = function() {

		var formData = new FormData($("#uploadForm")[0]);
		$.ajax({
			url:$scope.oBaseUrl+'hardware-service/api/excel/houseUploadByUnit.json',
			type: 'POST',
			data: formData,
			async: false,
			cache: false,
			contentType: false,
			processData: false,
			success: function(data) {
				//alert(JSON.parse(data).res.msg);
				$('#modal_3').modal('hide')
				$scope.alert({
					show: true,
					Title: '系统提示',
					Message: JSON.parse(data).res.msg,
					prevId: '#modal_3',
					Type: false
				});
				$('#ycf-alert').one('hidden.bs.modal', function(e) {
					document.location.reload()
				})

			},
			error: function(data) {
				//alert(JSON.parse(data).res.msg);
				$('#modal_3').modal('hide')
				$scope.alert({
					show: true,
					Title: '系统提示',
					Message: JSON.parse(data).res.msg,
					prevId: '#modal_3',
					Type: false
				});

			}
		});

	}

	//搜索
	$scope.sear_2 = function(pro, cty, dis, tric_nmb, adr_nmb, floor_nmb, unit_nmb, room_nmb) {
		//	 	console.info(JSON.parse(pro).pro_id)
		//	 	console.info(way+JSON.parse(pro).pro_id+JSON.parse(cty).city_id+JSON.parse(dis).dis_id);
		$scope.first_2 = true;
		$scope.allSear = {};
//		$scope.allSear.provinceId = pro;
//		$scope.allSear.cityId = cty;
//		$scope.allSear.dist = dis;
		$scope.allSear.provinceName = $filter('proName')(pro, $scope.prolist);
		$scope.allSear.cityName = $filter('cityName')(cty, $scope.oCityList);
		$scope.allSear.distName = $filter('disName')(dis, $scope.oDisList);
//		$scope.allSear.type = way;

		//	 	$scope.allSear.cityId =JSON.parse(cty).city_id;
		//	 	$scope.allSear.dist =JSON.parse(dis).dis_id;
		$scope.allSear.street = adr_nmb;
		$scope.allSear.districtName = tric_nmb;
		$scope.allSear.unit = unit_nmb;
		$scope.allSear.floorNo = floor_nmb;
		$scope.allSear.roomNo = room_nmb;
		$scope.allSear.numPerPage = 20;
		console.info($scope.allSear)

		listSve.sList().list($scope.allSear).then(function(e) {
			console.info(e)
			$scope.list_1 = e.res.data;
			var count = Math.ceil(e.res.page.recordCount / 20);
			$scope.count = count;
			pagerBox.innerHTML = "";
			var pager = new Pager({
				index: 1,
				total: $scope.count,
				parent: pagerBox,
				onchange: doChangePage_2
			});
		})
	}
	
	//搜索分页器
	function doChangePage_2(obj) {
		if($scope.first_2) {
			$scope.first_2 = false;
		} else {
			console.info(obj.index)
			$scope.allSear.pageNum = obj.index;
			//$scope.pageInx = {pageNum:obj.index,numPerPage:20,provinceId:9};
			//				$scope.getItem();
			listSve.sList().list($scope.allSear).then(function(e) {
				console.info(e);
				$scope.list_1 = e.res.data;

			})
		}
	}
	//点击设备
	$scope.hardwares = function(o, h) {
			if(o.type == "DIANBIAO") {
				$scope.mdl = '#modal_5';
				$scope.warsName = '电表';
				$scope.supname = o.ammeter.supplier.name;
				$scope.devId = o.ammeter.devId;
				$scope.ammeterId = o.ammeter.id;
				$scope.MName = o.ammeter.name;
//				$scope.hardwdress = $filter('proName')(h.houseStreet.provinceId, $scope.prolist) + $filter('cityName')(h.houseStreet.cityId, $scope.oCityList) + $filter('disName')(h.houseStreet.dist, $scope.oDisList) + h.houseStreet.street +$filter('floorZero')(h.floorNo)+$filter('unitZero')(h.unit)+ h.roomNo + "室" + h.door;
//				$scope.hardwdress2 = h.districtName+'   '+$filter('proName')(h.houseStreet.provinceId, $scope.prolist) + $filter('cityName')(h.houseStreet.cityId, $scope.oCityList) + $filter('disName')(h.houseStreet.dist, $scope.oDisList) + h.houseStreet.street +$filter('floorZero')(h.floorNo)+ $filter('unitZero')(h.unit) + h.roomNo + "室" + h.door;
				$scope.hardwdress = h.houseStreet.provinceName + h.houseStreet.cityName + h.houseStreet.distName + h.houseStreet.address+$filter('floorZero')(h.floorNo)+$filter('unitZero')(h.unit)+ h.roomNo + "室" + h.door;
				$scope.hardwdress2 = h.districtName+'   '+h.houseStreet.provinceName + h.houseStreet.cityName + h.houseStreet.distName +h.houseStreet.address+$filter('floorZero')(h.floorNo)+ $filter('unitZero')(h.unit) + h.roomNo + "室" + h.door;
				$scope.install = o.ammeter.installTime
				$scope.status = {};
				$scope.status.devId = o.ammeter.devId;
				listSve.zStatus().list($scope.status).then(function(e) {
					console.log(e)
					if(e.code == 1) {
						$scope.alert({
							show: true,
							Title: '系统提示',
							Message: e.res.msg,
							prevId: '',
							Type: false
						});
					} else {
						$('#modal_5').modal('show')
						var oId = o.type+h.id;
						angular.element("#" + oId).attr({
							'class': e.res.data.hardwareStatus
						});
						console.info(angular.element("#" + oId))
						//在线状态 "wifiOnlineStatus": "on",on 在线 off 离线
						$scope.hardwareStatus_1 = e.res.data.wifiOnlineStatus;
						$scope.payMod = e.res.data.mode;;
						$scope.electricityPrice = e.res.data.price;
						//通电状态electricDoorSwitchStatus  on 通电 off断电
						$scope.hardwareStatus_2 = e.res.data.electricDoorSwitchStatus;
						if($scope.hardwareStatus_1 == 'off') {
							$scope.clor = "red";
						}else{
							$scope.clor = "";
						};
						if($scope.hardwareStatus_2 == 'off') {
							$scope.clor_t = "red";
						}else{
							$scope.clor_t = "";
						};
						if($scope.payMod == 0) {
							$scope.clor_y = "red";
						}else{
							$scope.clor_y = "";
						};
					}

				})

			} else if(o.type == "MENSUO") {
				$scope.mdl = '#modal_4';
				$scope.warsName = '门锁';
				$scope.supname_1 = o.doorLock.supplier.name;
				$scope.devId_1 = o.doorLock.devId;
				$scope.doorLockId = o.doorLock.id;
//				$scope.hardwdress_1 = $filter('proName')(h.houseStreet.provinceId, $scope.prolist) + $filter('cityName')(h.houseStreet.cityId, $scope.oCityList) + $filter('disName')(h.houseStreet.dist, $scope.oDisList) + h.houseStreet.street + $filter('floorZero')(h.floorNo)+ $filter('unitZero')(h.unit) + h.roomNo + "室" + h.door;
//				$scope.hardwdress_1_1 = h.districtName+'   '+ $filter('proName')(h.houseStreet.provinceId, $scope.prolist) + $filter('cityName')(h.houseStreet.cityId, $scope.oCityList) + $filter('disName')(h.houseStreet.dist, $scope.oDisList) + h.houseStreet.street + $filter('floorZero')(h.floorNo)+ $filter('unitZero')(h.unit) + h.roomNo + "室" + h.door;
				$scope.hardwdress_1 = h.houseStreet.provinceName + h.houseStreet.cityName + h.houseStreet.distName + h.houseStreet.address+ $filter('floorZero')(h.floorNo)+ $filter('unitZero')(h.unit) + h.roomNo + "室" + h.door;
				$scope.hardwdress_1_1 = h.districtName+'   '+ h.houseStreet.provinceName + h.houseStreet.cityName + h.houseStreet.distName + h.houseStreet.address+ $filter('floorZero')(h.floorNo)+ $filter('unitZero')(h.unit) + h.roomNo + "室" + h.door;

				//doorList
				$scope.door = {};
				$scope.door.devId = o.doorLock.devId;
				$scope.door.type = 'MENSUO';
				listSve.doorList().list($scope.door).then(function(e) {
					console.log(e)
					if(e.code == 1) {
						//					$scope.alert = {Message:e.res.msg,Title:'系统提示',Type:false}
						//					$('#ycf-alert').modal('show')
						$scope.alert({
							show: true,
							Title: '系统提示',
							Message: e.res.msg,
							prevId: '',
							Type: false
						});
					} else {
						$('#modal_4').modal('show')
						var oId_1 = o.type+h.id;
						$("#" + oId_1).attr({
							'class': e.res.data.hardwareStatus
						});
//					      "hardwareStatus": "ZC",硬件综合状态
//					      "wifiOnlineStatus": "on",on 在线 off 离线
//					      "powerLeftValue": 100门锁电池剩余
						$scope.hardwareStatus_4 = e.res.data.powerLeftValue;
						$scope.hardwareStatus_3 = e.res.data.wifiOnlineStatus; //在线
						if(e.res.data.powerLeftValue < 20) {
							$scope.clor_2 = "red";
						}else{
							$scope.clor_2 = '';
						}
						if(e.res.data.wifiOnlineStatus == "off") {
							$scope.clor_1 = "red";
						}else{
							$scope.clor_1 = "";
						}
					}
				})
				$scope.install_1 = o.doorLock.installTime

			}

		}
	
	
	
	//电表联网历史
	$scope.powerHistory = function(id,drs){
		$scope.history_1 = true;
		$('#modal_5').modal('hide')
		$('#modal_5').one('hidden.bs.modal', function(e) {
			$('#modal_5_2').modal('show')
		})
		$scope.historyDress = drs;
		console.log(id,drs)
		//周联网历史
		$scope.line_w = {};
		$scope.line_w.ammeterId = id;
		$scope.line_w.type = 'week';
		listSve.lineHistory().list($scope.line_w).then(function(e) {
				console.log(e);
				$scope.oline_w = e.res.data;
				
			})
		//周联网历史
		$scope.line_m = {};
		$scope.line_m.ammeterId = id;
		$scope.line_m.type = 'month';
		listSve.lineHistory().list($scope.line_m).then(function(e) {
				console.log(e);
				$scope.oline_m = e.res.data;
			})
		
		
	};
	//点击切换周月用设备历史
	$scope.oWeek_2 = function() {
		$scope.history_1 = true;
	};
	$scope.oMonth_2 = function() {
		$scope.history_1 = false;
	};
	
	
	
	
		//电量历史
	$scope.power = function(e, d) {
		$scope.chart = true;
		$('#modal_5').modal('hide')
		$('#modal_5').one('hidden.bs.modal', function(e) {
			$('#modal_5_1').modal('show')
		})
		console.log(e, d)
			//powerList
		$scope.power_e = {};
		$scope.power_e.devId = e;
		$scope.power_e.type = 'week';
		listSve.powerList().list($scope.power_e).then(function(e) {
				var txt = d;
				console.log(e);
				//电量列表
				$scope.power_list = e.res.data.data;
				var dataName = e.res.data.data;
				var oDataName = [];
				var oData = [];
				for(var i = 0; i < dataName.length; i++) {
					oDataName.unshift(dataName[i].Date)
						//				oData.unshift(dataName[i].Allpower.toFixed(2))
					oData.unshift(Math.floor((dataName[i].Allpower) * 100) / 100)
				}
				console.info(oDataName)
					// 基于准备好的dom，初始化echarts实例
				var myChart = echarts.init(document.getElementById('chart'));
				// 指定图表的配置项和数据
				var option = {
					title: {
						text: txt,
						subtext: '单位 ：度'
					},
					tooltip: {
						trigger: 'axis'
					},
					xAxis: {
						data: oDataName
					},
					yAxis: {},
					series: [{
						name: '电量（度）',
						type: 'line',
						data: oData
					}]
				};
				// 使用刚指定的配置项和数据显示图表。
				myChart.innerHTML = "";
				myChart.setOption(option);
			})
			//月用电量数据---------------------------------------//		
		$scope.power_1 = {};
		$scope.power_1.devId = e;
		$scope.power_1.type = 'month';
		listSve.powerList().list($scope.power_1).then(function(e) {
			var txt = d;
			console.log(e);
			//月用电量列表
			$scope.power_list_1 = e.res.data.data;
			var dataName = e.res.data.data;
			var oDataName = [];
			var oData = [];
			for(var i = 0; i < dataName.length; i++) {
				oDataName.unshift(dataName[i].Date)
					//				oData.unshift(dataName[i].Allpower)
				oData.unshift(Math.floor((dataName[i].Allpower) * 100) / 100)
			}
			console.info(oDataName)
				// 基于准备好的dom，初始化echarts实例
			var myChart_1 = echarts.init(document.getElementById('chart_1'));
			// 指定图表的配置项和数据
			var option = {
				title: {
					text: txt,
					subtext: '单位 ：度'
				},
				tooltip: {
					trigger: 'axis'
				},
				xAxis: {
					data: oDataName
				},
				yAxis: {},
				series: [{
					name: '电量（度）',
					type: 'line',
					data: oData
				}]
			};
			// 使用刚指定的配置项和数据显示图表。
			myChart_1.innerHTML = "";
			myChart_1.setOption(option);
		})

	};

	//点击切换周月用电详情
	$scope.oWeek = function() {
		$scope.chart = true;
	};
	$scope.oMonth = function() {
		$scope.chart = false;
	};

	//断电通电按钮
	$scope.loop = function(e, s) {

			$scope.powerloop = {};
			$scope.powerloop.devId = e;
			if(s == 'on') {
				$scope.powerloop.action = "off";
			} else if(s == 'off') {
				$scope.powerloop.action = "on";
			}

			listSve.powerLoop().list($scope.powerloop).then(function(e) {
				//成功后提示断电成功
				$('#modal_5').modal('hide')
				console.info(e)
				if(e.code == 0) {
					if(s == 'on') {
						$scope.hardwareStatus_2 = 'off';
						//					$scope.hardwareStatus_1 = 0;
						$scope.clor_t = "red";
						$scope.alert({
							show: true,
							Title: '系统提示',
							Message: e.res.msg,
							prevId: '#modal_5',
							Type: false
						});
						$scope.calback = function() {
							document.location.reload()
						}
					} else if(s == 'off') {
						$scope.hardwareStatus_2 = 'on';
						//$scope.hardwareStatus_1 = 1;
						$scope.clor_t = "";
						$scope.alert({
							show: true,
							Title: '系统提示',
							Message: e.res.msg,
							prevId: '#modal_5',
							Type: false
						});
						$scope.calback = function() {
							document.location.reload()
						}
					}
				} else {
					$scope.alert({
						show: true,
						Title: '系统提示',
						Message: e.res.msg,
						prevId: '#modal_5',
						Type: false
					});
				}
			})
		};
	
	//门锁联网历史
	$scope.doorHistory = function(id,drs){
		$scope.history_3 = true;
		$('#modal_4').modal('hide')
		$('#modal_4').one('hidden.bs.modal', function(e) {
			$('#modal_4_2').modal('show')
		})
		$scope.historyDress_1 = drs;
		console.log(id,drs)
		$scope.line_w_1 = {};
		$scope.line_w_1.doorLockId = id;
		$scope.line_w_1.type = 'week';
		listSve.lineHistory().list($scope.line_w_1).then(function(e) {
				console.log(e);
				$scope.oline_w_1 = e.res.data;
				
			})
		//周联网历史
		$scope.line_m_1 = {};
		$scope.line_m_1.doorLockId = id;
		$scope.line_m_1.type = 'month';
		listSve.lineHistory().list($scope.line_m_1).then(function(e) {
				console.log(e);
				$scope.oline_m_1 = e.res.data;
			})
	};
	//点击切换周月用设备历史
	$scope.oWeek_3 = function() {
		$scope.history_3 = true;
	};
	$scope.oMonth_3 = function() {
		$scope.history_3 = false;
	};
	
	//低电量历史lowPowHistory
	$scope.lowPowerHistory = function(id,drs){
		$scope.history_4 = true;
		$('#modal_4').modal('hide')
		$('#modal_4').one('hidden.bs.modal', function(e) {
			$('#modal_4_3').modal('show')
		})
		$scope.historyDress_2 = drs;
		console.log(id,drs)
		$scope.line_p_w = {};
		$scope.line_p_w.doorLockId = id;
		$scope.line_p_w.type = 'week';
		listSve.lowPowHistory().list($scope.line_p_w).then(function(e) {
				console.log(e);
				$scope.oline_p_w = e.res.data;
				
			})
		//周联网历史
		$scope.line_p_m = {};
		$scope.line_p_m.doorLockId = id;
		$scope.line_p_m.type = 'month';
		listSve.lowPowHistory().list($scope.line_p_m).then(function(e) {
				console.log(e);
				$scope.oline_p_m = e.res.data;
			})
	};
	//点击切换周月用设备历史
	$scope.oWeek_4 = function() {
		$scope.history_4 = true;
	};
	$scope.oMonth_4 = function() {
		$scope.history_4 = false;
	};
	
	
		//清除所有密码
	$scope.oClear = function(e) {

		$scope.doorClear = {};
		$scope.doorClear.devId = e;
		listSve.doorClear().list($scope.doorClear).then(function(e) {
			$('#modal_4').modal('hide')
				//成功后提示清除成功
			if(e.code == 0) {
				$scope.alert({
					show: true,
					Title: '系统提示',
					Message: e.res.msg,
					prevId: '#modal_4',
					Type: false
				});
			} else {
				$scope.alert({
					show: true,
					Title: '系统提示',
					Message: e.res.msg,
					prevId: '#modal_4',
					Type: false
				});
			}
			console.info(e)
		})
	}

	//开门记录openDoor
	//var pagerBox_1 = document.getElementById('pager_3');
	//var pagerBox_1_1 = document.getElementById('pager_3_1');
	$scope.openDoor = function(e, d) {
			$scope.history = true;
			//data-dismiss="modal" data-toggle="modal" data-target="#modal_4_1"
			$('#modal_4').modal('hide')
			$('#modal_4').one('hidden.bs.modal', function(e) {
					$('#modal_4_1').modal('show')
				})
				//周开门记录
			$scope.doorClear_1 = {};
			$scope.doorClear_1.devId = e;
			$scope.doorClear_1.numPerPage = 5;
			$scope.doorClear_1.type = 'week';
			$scope.openDress = d;
			listSve.doorClear_1().list($scope.doorClear_1).then(function(e) {
					console.info(e)
					$scope.openlist_1 = e.res.data;
					//pager_3
//					var count = Math.ceil(e.res.page.recordCount / 20);
//					$scope.count_1 = count;
//					pagerBox_1.innerHTML = "";
//					var pager = new Pager({
//						index: 1,
//						total: $scope.count_1,
//						parent: pagerBox_1,
//						onchange: doChangePage_1
//					});
				})
				//月开门记录
			$scope.doorClear = {};
			$scope.doorClear.devId = e;
			$scope.doorClear.numPerPage = 20;
			$scope.doorClear.type = 'month';
			$scope.openDress = d;
			listSve.doorClear_1().list($scope.doorClear).then(function(e) {
				console.info(e)
				$scope.openlist = e.res.data;
				//pager_3
//				var count = Math.ceil(e.res.page.recordCount / 20);
//				$scope.count_1_1 = count;
//				pagerBox_1_1.innerHTML = "";
//				var pager = new Pager({
//					index: 1,
//					total: $scope.count_1_1,
//					parent: pagerBox_1_1,
//					onchange: doChangePage_1_1
//				});
			})

		}
		//点击切换周月开门记录
	$scope.oWeek_1 = function() {
		$scope.history = true;
	};
	$scope.oMonth_1 = function() {
		$scope.history = false;
	};
	//分页器
//	$scope.second = true;
//
//	function doChangePage_1(obj) {
//		if($scope.second) {
//			$scope.second = false;
//		} else {
//			console.info(obj.index)
//				//,numPerPage:1
//			$scope.pageInx = {
//				pageNum: obj.index
//			};
//			listSve.doorClear().list($scope.pageInx).then(function(e) {
//				console.info('点击分页器返回数据' + e);
//				$scope.list = e.res.data;
//
//			})
//		}
//	}
//	$scope.second_1 = true;
//
//	function doChangePage_1_1(obj) {
//		if($scope.second_1) {
//			$scope.second_1 = false;
//		} else {
//			console.info(obj.index)
//				//,numPerPage:1
//			$scope.pageInx = {
//				pageNum: obj.index
//			};
//			listSve.doorClear().list($scope.pageInx).then(function(e) {
//				console.info('点击分页器返回数据' + e);
//				$scope.list = e.res.data;
//
//			})
//		}
//	}

	//租客信息
	$scope.renter = function(e) {
		$scope.oRenter = e;

	}

}])